package com.library.service.student;

import com.library.dao.LibcardDao;
import com.library.entity.Libcard;

import java.sql.Date;

public class LibcardService {
    private LibcardDao libcardDao = new LibcardDao();

    // 查询借书证
    public Libcard getLibCard(String sno) {
        return libcardDao.getBySno(sno);
    }
    // 新办借书证
    public String applyLibCard(String sno, String cardNo, String sname, String type, String collage, String major, Date birth, String originPlace) {
        libcardDao.insertLibcard(cardNo, sno, sname, type, collage, major, birth, originPlace);
        libcardDao.insertCardrec(sno, cardNo, "新办");
        return "借书证新办成功！";
    }
    // 挂失借书证
    public String reportLossLibCard(String sno) {
        libcardDao.updateStatus(sno, "挂失");
        Libcard card = libcardDao.getBySno(sno);
        if (card != null) {
            libcardDao.insertCardrec(sno, card.getCardNo(), "挂失");
        }
        return "借书证挂失成功！";
    }
    // 注销借书证
    public String cancelLibCard(String sno) {
        libcardDao.updateStatus(sno, "注销");
        Libcard card = libcardDao.getBySno(sno);
        if (card != null) {
            libcardDao.insertCardrec(sno, card.getCardNo(), "注销");
        }
        return "借书证注销成功！";
    }
    // 补办借书证
    public String reissueLibCard(String sno) {
        Libcard card = libcardDao.getBySno(sno);
        if (card == null) {
            return "无借书证，无法补办！";
        }
        String status = card.getCardType();
        if (!"挂失".equals(status) && !"注销".equals(status)) {
            return "只有挂失或注销状态的借书证才能补办！";
        }
        String oldCardNo = card.getCardNo();
        libcardDao.updateStatusByCardNo(oldCardNo, "正常");
        libcardDao.insertCardrec(sno, oldCardNo, "补办");
        return "借书证补办成功！";
    }
} 